package com.yaorange.controller;

import com.github.pagehelper.PageInfo;
import com.yaorange.api.Result;
import com.yaorange.entity.Apply;
import com.yaorange.entity.Article;
import com.yaorange.entity.Parmsz;
import com.yaorange.service.ApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @company: yaorange
 * @author: 啊啊啊林
 * @version: 1.0
 * @create: 2021-09-26 19:45:32
 */
@RestController
@RequestMapping("/apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;


    //查询表格里面的数据  申请状态 为0
    @GetMapping("/getAll")
    public Result<?> getAllApply() {
        Result<List<Apply>> listResult = new Result<>();

        List<Apply> applies = applyService.selectApplyDate(0);
        listResult.setData(applies);
        listResult.setSuccess(true);
        return listResult;
    }


    @GetMapping("/page/{currentPage}/{pageSize}")
    public Result<?> getPageApply(@PathVariable Integer currentPage,@PathVariable Integer pageSize) {
        Result<PageInfo<Apply>> listResult = new Result<>();
     PageInfo<Apply> pageInfo=   applyService.selectApplyByPage(currentPage, pageSize);
        listResult.setSuccess(true);
        listResult.setData(pageInfo);

        return listResult;
    }
//条件分页查询
    @GetMapping ("/conditions")
    public Result<?> conditionSelect(Integer userId) {
        Result<PageInfo<Apply>> pageInfoResult = new Result<>();
    PageInfo<Apply> pageInfo=    applyService.selectApplyByPageOne(userId);
        System.out.println(userId);

        System.out.println("hh");

        pageInfoResult.setSuccess(true);
        pageInfoResult.setData(pageInfo);

        return pageInfoResult;
    }

    //改变申请状态
    @GetMapping("/{applyId}/{stateId}")
    public Result<?> changeApplyState(@PathVariable Integer applyId,@PathVariable Integer stateId) {
        Result<Object> result = new Result<>();
        int i = applyService.updateApplyState(applyId, stateId);
        if (i > 0) {
            result.setSuccess(true);
            result.setMessage("修改成功");
            return result;
        }
        return result;
    }

    @PostMapping("/newApply")
    public Result<?> addNewApply( @RequestBody Apply apply) {
        if (apply.getApplyUnit().contains("本")) {
            String substring = apply.getApplyUnit().substring(2);
            apply.setApplyUnit(substring);
        }
        apply.setApplyState(0);

        int row = applyService.insertNewApply(apply);

        Result<Object> result = new Result<>();
        return result;
    }

    @GetMapping("/person/{userId}")
    public Result<?> queryApplyPersonByUserId(@PathVariable Integer userId) {
        Result<Object> result = new Result<>();
        PageInfo<Apply> pageInfo = applyService.selectApplyByPageOne(userId);
        return result;
    }
    //申请列表页面表格数据
    @GetMapping("/personal/{userId}/{currentPage}")
    public Result<?> queryApplyPerson(String goodsName, Integer typeId,@PathVariable Integer currentPage,@PathVariable Integer userId) {
        PageInfo<Apply> result = applyService.selectApplyList(userId, currentPage,goodsName,typeId);
        Result<PageInfo<Apply>> pageInfoResult = new Result<>();
        pageInfoResult.setData(result);
        return pageInfoResult;

    }

    @DeleteMapping("/personal/{applyId}")
    public Result<?> deleteApplyByApplyId(@PathVariable Integer applyId) {

        int i = applyService.updateApplyState(applyId, 2);
        Result<Object> result = new Result<>();
        if (i > 0) {
            result.setSuccess(true);

        }
        return result;
    }

    //前端页面修改申请功能
    @GetMapping("/personal/up")
    public Result<?> updateApply(Integer applyId,String applyReason,Integer applyNum) {
        Result<Object> result = new Result<>();
        int row = applyService.updateApplyPerson(applyId, applyReason, applyNum);
        if (row > 0) {
            result.setSuccess(true);

        }
        return result;
    }

    //批量撤回
    @PostMapping("/withdraw")
    public Result<?> withdrawMany(@RequestBody  Integer str[]) {
        System.out.println("hh");
        for (int i = 0; i < str.length; i++) {
            System.out.println(str[i]);
        }

        Result<Object> result = new Result<>();
        int row= applyService.updateApplyStateMany(str);
        if (row > 0) {
            result.setSuccess(true);
            return result;
        }
        return result;
    }

}
